﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Text;
using System.Windows.Markup;
using System.IO;

namespace SmartText
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public class LangInfo
        {
            public string Language { get; set; }
            public string Lang { get; set; }
        }

        public AppInfo Info { get; set; }
        public List<LangInfo> LanguageList { get; set; }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            Info = new AppInfo();
            if (e.Args.Length > 0)
            {
                SetInfo(e);
            }
            FillLanguageList();
            new SmartText.MainWindow().ShowDialog();
            this.Shutdown(Info.OutputSuccess ? 0 : 1);
        }

        private void FillLanguageList()
        {
            var path = string.Format(@"{0}\Language\",
                System.Windows.Forms.Application.StartupPath);

            LanguageList = new List<LangInfo>();
            foreach (var file in Directory.GetFiles(path, "*.xaml"))
            {
                var resDict = XamlReader.Load(new FileStream(file, FileMode.Open)) as ResourceDictionary;
                var li = new LangInfo(){Language=resDict["lang"].ToString(), Lang=Path.GetFileNameWithoutExtension(file)};
                LanguageList.Add(li);
            }
            LanguageList = LanguageList.OrderBy(li => li.Language).ToList();
        }

        private void SetInfo(StartupEventArgs e)
        {
            var count = e.Args.Length;
            int enc;

            Info.HasCommandLineArgs = true;
            if (count >= 1)
            {
                Info.Template = e.Args[0];
            }
            if (count >= 2)
            {
                Info.Param = e.Args[1];
            }
            if (count >= 3)
            {
                Info.Output = e.Args[2];
            }
            if (count >= 4)
            {
                if (int.TryParse(e.Args[3], out enc))
                {
                    Info.TemplateEncoding = Encoding.GetEncoding(enc);
                }
            }
            if (count >= 5)
            {
                if (int.TryParse(e.Args[4], out enc))
                {
                    Info.ParamEncoding = Encoding.GetEncoding(enc);
                }
            }
            if (count >= 6)
            {
                if (int.TryParse(e.Args[5], out enc))
                {
                    Info.OutputEncoding = Encoding.GetEncoding(enc);
                }
            }
        }

        static public string GetLanguageText(string key)
        {
            return Application.Current.FindResource(key).ToString();
        }

        static public bool LoadLanguage(string lang)
        {
            var file = string.Format(@"{0}\Language\{1}.xaml",
                System.Windows.Forms.Application.StartupPath, lang);

            if (!File.Exists(file))
            {
                MessageBox.Show(String.Format("Cannot find language file {0}", file), Application.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Stop);
                return false;
            }

            var resDict = XamlReader.Load(new FileStream(file, FileMode.Open)) as ResourceDictionary;
            if (Application.Current.Resources.MergedDictionaries.Count > 1)
            {
                Application.Current.Resources.MergedDictionaries.RemoveAt(1);
            }
            Application.Current.Resources.MergedDictionaries.Add(resDict);
            return true;
        }
    }
}
